package com.ssm.controller;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jfinal.plugin.activerecord.Record;
import com.ssm.pojo.InvokeOpenApiParam2;
import com.ssm.service.CommonService;
import com.ssm.utils.ResultInfoUtil;

import util.JFinalUtils;

@Controller
@RequestMapping("/common/index")
public class CommonRouterController {
	private static final Logger logger = LoggerFactory.getLogger(CommonRouterController.class);
	
	private static final String FORMAT_JSON = "json";
	private static final String FORMAT_XML = "xml";

	@Autowired
	private CommonService commonService;
	
	@RequestMapping(value = "/testStep", method = { RequestMethod.POST, RequestMethod.GET })
	public String testStep(HttpServletRequest request,HttpServletResponse response, ModelMap map) {
		logger.info("redirect view/testStep2.jsp");
		
		map.put("RESTURL_DEV", JFinalUtils.propDev.get("RESTURL"));
		map.put("WSURL_DEV", JFinalUtils.propDev.get("WSURL"));
		//
		map.put("RESTURL_UAT", JFinalUtils.propUat.get("RESTURL"));
		map.put("WSURL_UAT", JFinalUtils.propUat.get("WSURL"));
		//
		map.put("RESTURL_PRD", JFinalUtils.propPrd.get("RESTURL"));
		map.put("WSURL_PRD", JFinalUtils.propPrd.get("WSURL"));
		
		return "testStep2";
	}
	
	@RequestMapping(value = "/testStepAdmin", method = { RequestMethod.POST, RequestMethod.GET })
	public String testStepAdmin(HttpServletRequest request,HttpServletResponse response, ModelMap map) {
		logger.info("redirect view/testStepAdmin.jsp");
		
		map.put("RESTURL_DEV", JFinalUtils.propDev.get("RESTURL"));
		map.put("WSURL_DEV", JFinalUtils.propDev.get("WSURL"));
		//
		map.put("RESTURL_UAT", JFinalUtils.propUat.get("RESTURL"));
		map.put("WSURL_UAT", JFinalUtils.propUat.get("WSURL"));
		//
		map.put("RESTURL_PRD", JFinalUtils.propPrd.get("RESTURL"));
		map.put("WSURL_PRD", JFinalUtils.propPrd.get("WSURL"));
		
		return "testStepAdmin";
	}
	
	@RequestMapping(value = "/testStepLoad", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object testStepLoad(HttpServletRequest request,HttpServletResponse response, 
			@RequestParam(name="apiId",required=true) String apiId, 
			@RequestParam(name="appSubId",required=true)String appSubId,
			@RequestParam(name="env",required=true)String env) {
		List<Record> list = JFinalUtils.fetchApiInfo(apiId, appSubId, env);
		if(null != list && list.size() > 0){
			Record rd = list.get(0);
			Map<String,Object> map = new HashMap<String,Object>();
			for(String key : rd.getColumns().keySet()){
				map.put(key, rd.get(key));
			}
			return ResultInfoUtil.success(map);
		}else{
			return ResultInfoUtil.error("query error!");
		}
	}
	
	
	@RequestMapping(value = "/testStepSubmit", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object testStepSubmit(InvokeOpenApiParam2 param, HttpServletRequest request,HttpServletResponse response) {
		if(param == null){
			return ResultInfoUtil.error("InvokeOpenApiParam2 is null");
		}
		System.out.println( param );
		if(null == param.getUrl()){
			return ResultInfoUtil.error("url is null");
		}
		if(null == param.getFormat()){
			return ResultInfoUtil.error("Format is null");
		}
		Field[] fields = InvokeOpenApiParam2.class.getDeclaredFields();
		try {
			for(Field field : fields){
				String str = BeanUtils.getProperty(param, field.getName());
				if(null == str || str.trim().equals("")){
					if("signMethod".equals(field.getName()) || "privateKey".equals(field.getName())){
						continue;
					}
					return ResultInfoUtil.error( field.getName() + " is null");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		Map<String,Object> resultMap = null;
		if(param.getUrl().endsWith("rs-service/")){
			if(FORMAT_JSON.equals(param.getFormat())){
				resultMap = commonService.invokeWithRestJson(param);
			}
			if(FORMAT_XML.equals(param.getFormat())){
				resultMap = commonService.invokeWithRestXml(param);
			}
		}
		if(param.getUrl().endsWith("ws-service/")){
			resultMap = commonService.invokeWithSoap(param);
		}
		
		
		logger.info("redirect view/testStep.jsp");
		return ResultInfoUtil.success(resultMap);
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/showConfig", method = { RequestMethod.POST, RequestMethod.GET })
	public String showConfig(HttpServletRequest request,HttpServletResponse response, ModelMap map) {
		logger.info("showConfig");
		return "showConfig";
	}
	
}
